/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package progin;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

import org.json.*;

/**
 *
 * @author Jiwandono
 */
public class listServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            ServletConfig scfg = getServletConfig();
            ServletContext sc = scfg.getServletContext();
            chatData cd = (chatData) sc.getAttribute("ichat.chatdata");

            Class.forName("com.mysql.jdbc.Driver");

            dbConfig dbc = new dbConfig();
            Connection con = DriverManager.getConnection(dbc.url, dbc.username, dbc.password);

            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT DATE(NOW())"); // testing

            if(request.getParameter("action").equals("getlist")) {
              JSONObject jsonData = new JSONObject();

              JSONArray jsonDataFakultas = new JSONArray();
              rs = st.executeQuery("SELECT * FROM `fakultas`");
              while(rs.next()) {
                JSONObject fakultas = new JSONObject();
                fakultas.put("idfakultas", rs.getString("idfakultas"));
                fakultas.put("nama", rs.getString("nama"));
                jsonDataFakultas.put(fakultas);
              }
              
              JSONArray jsonDataProdi = new JSONArray();
              rs = st.executeQuery("SELECT * FROM `prodi`");
              while(rs.next()) {
                JSONObject prodi = new JSONObject();
                prodi.put("idfakultas", rs.getString("idfakultas"));
                prodi.put("idprodi", rs.getString("idprodi"));
                prodi.put("nama", rs.getString("nama"));
                jsonDataProdi.put(prodi);
              }

              JSONArray jsonDataRoom = new JSONArray();
              int n = 0;
              while(n < cd.room.size()) {
                  JSONObject room = new JSONObject();
                  room.put("name", cd.room.get(n).name);
                  room.put("parent", cd.room.get(n).parent);
                  room.put("owner", cd.room.get(n).owner);
                  room.put("parent", cd.room.get(n).parent);
                  room.put("parenttype", cd.room.get(n).parenttype);
                  room.put("roomtype", cd.room.get(n).roomtype);
                  room.put("timestamp", cd.room.get(n).timestamp.toString());
                  jsonDataRoom.put(room);
                  n++;
              }

              jsonData.put("fakultas", jsonDataFakultas);
              jsonData.put("prodi", jsonDataProdi);
              jsonData.put("room", jsonDataRoom);

              out.println(jsonData.toString());
            }

            rs.close();
            st.close();
            con.close();
        } catch(SQLException ex) {
            while (ex != null) {
                out.println ("SQL Exception:  " + ex.getMessage ());
                ex = ex.getNextException ();
            }
        } catch(Exception ex) {
            if(ex != null) {
                out.println ("Exception:  " + ex.getMessage ());
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

